package com.ruoyi.web.controller.tool;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.OperatorType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysClientWeight;
import com.ruoyi.system.domain.UpWeight;
import com.ruoyi.system.service.ISysClientWeightService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

/**
 * 智能称重记录Controller
 * 
 * @author spark
 * @date 2023-08-13
 */
@ApiOperation("智能称重")
@RestController
@RequestMapping("/api/Records")
@Anonymous
public class TestClientWeightController extends BaseController
{
    @Autowired
    private ISysClientWeightService sysClientWeightService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询智能称重记录列表
     */
    @ApiOperation("查询智能称重记录列表")
    @GetMapping("/list")
    public TableDataInfo list(SysClientWeight sysClientWeight)
    {
        startPage();
        List<SysClientWeight> list = sysClientWeightService.selectSysClientWeightList(sysClientWeight);
        return getDataTable(list);
    }


    /**
     * 获取智能称重记录详细信息
     */
    @ApiOperation("获取智能称重记录详细信息")
    @GetMapping(value = "/{dataId}")
    public AjaxResult getInfo(@PathVariable("dataId") Long dataId)
    {
        return success(sysClientWeightService.selectSysClientWeightByDataId(dataId));
    }

    /**
     * 新增智能称重记录
     */
    @ApiOperation("新增智能称重记录")
    @Log(title = "智能称重记录", businessType = BusinessType.OTHER ,operatorType= OperatorType.CLIENT)
    @PostMapping
    @Anonymous
    public AjaxResult upw(@RequestBody UpWeight upWeight)
    {

        int i = -1;
        try{
        SysClientWeight sysClientWeight = new SysClientWeight();
            if (upWeight.getWatchDate() == null) {
                new AjaxResult(71, "失败：上传时间不能为空！", null);
            }
            String uploadTimeStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, upWeight.getWatchDate());
            sysClientWeight.setProp2(uploadTimeStr);
            sysClientWeight.setUploadTime(upWeight.getWatchDate());
        if(StringUtils.isNotEmpty(upWeight.getUserCode())) {
            Long userId = Long.parseLong(upWeight.getUserCode());
            SysUser sysUser = userService.selectUserById(userId);
            if(sysUser!=null){
                sysClientWeight.setUserId(userId);
                sysClientWeight.setUserName(sysUser.getUserName());
                sysClientWeight.setNickName(sysUser.getNickName());
            }else{
                new AjaxResult(72, "失败：UserCode无效，账户不存在！", null);
            }
        }

        sysClientWeight.setGoodsName(upWeight.getInvName());
        if(StringUtils.isNotEmpty(upWeight.getShedNo()))
        sysClientWeight.setClientId(Long.parseLong(upWeight.getShedNo()));
        if(StringUtils.isNotEmpty(upWeight.getMushroomLevel()))
        sysClientWeight.setWorkLineId(Long.parseLong(upWeight.getMushroomLevel()));
        if(StringUtils.isNotEmpty(upWeight.getWeight()))
        sysClientWeight.setWeight(new BigDecimal(upWeight.getWeight()));
        sysClientWeight.setCreateBy(upWeight.getUserCode());
        if(StringUtils.isNotEmpty(upWeight.getAllWeight()))
        sysClientWeight.setAllWeight(new BigDecimal(upWeight.getAllWeight()));
            if(StringUtils.isNotEmpty(upWeight.getSubWeight()))
        sysClientWeight.setSubWeight(new BigDecimal(upWeight.getSubWeight()));
            if(StringUtils.isNotEmpty(upWeight.getSingleWeight()))
        sysClientWeight.setSingleWeight(new BigDecimal(upWeight.getSingleWeight()));
        sysClientWeight.setSingleUnit(upWeight.getSingleUnit());
        sysClientWeight.setGoodsId(upWeight.getInvId());
        sysClientWeight.setWeightUnit(upWeight.getWeightUnit());
        sysClientWeight.setAddress(upWeight.getAddress());
        sysClientWeight.setAmount(upWeight.getAmount());
        sysClientWeight.setAmountUnit(upWeight.getAmountUnit());
            if(StringUtils.isNotEmpty(upWeight.getCheckFloor()))
        sysClientWeight.setCheckFloor(new BigDecimal(upWeight.getCheckFloor()));
            if(StringUtils.isNotEmpty(upWeight.getCheckUp()))
        sysClientWeight.setCheckUp(new BigDecimal(upWeight.getCheckUp()));
        //sysClientWeight.setPass(upWeight.getPass());
        sysClientWeight.setProp1(upWeight.getSerialNumber());
        sysClientWeight.setDataType(upWeight.getDataType());
            if(StringUtils.isNotEmpty(upWeight.getFactoryId()))
        sysClientWeight.setFactoryId(Long.parseLong(upWeight.getFactoryId()));
            if(StringUtils.isNotEmpty(upWeight.getRoomId()))
        sysClientWeight.setRoomId(Long.parseLong(upWeight.getRoomId()));
        i = sysClientWeightService.inserttWeightReqSeqId(sysClientWeight);
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(-1, "失败："+e.getMessage(), null);
        }
        if (i>0) {
            return new AjaxResult(1, "成功", null);
        }else if(i== -7){
            return new AjaxResult(70, "失败：重复上传称重记录", null);
        }else {
            return new AjaxResult(0, "失败", null);
        }

    }


}
